<template>
  <div>
    <el-input v-if="props.searchType === 1" :style="{ width: '200px'}" readonly v-model="pd.dataText"
              :placeholder="props.title">
      <template #prepend>
        <el-button icon="Search" @click="initSearch"></el-button>
      </template>
    </el-input>
    <el-button v-if="props.searchType === 2" type="primary" @click="initSearch">{{ props.dataText }}</el-button>
    <el-link v-if="props.searchType === 3" type="primary" @click="initSearch">{{ pd.dataText }}</el-link>
    <el-link v-if="props.searchType === 4" type="primary" @click="initSearch">{{ props.dataText }}</el-link>
    <el-dialog :title="props.title" v-model="pd.open" width="900px" append-to-body>
      <el-form :model="pd.queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="工序名称" prop="name">
          <el-input v-model="pd.queryParams.name" placeholder="请输入工序名称" clearable style="width: 240px"
                    @keyup.enter="handleQuery"/>
        </el-form-item>
        <el-form-item label="工艺类别" prop="treeValue">
          <el-input v-model="pd.queryParams.treeValue" placeholder="请输入工艺类别" clearable style="width: 240px"
                    @keyup.enter="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重 置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-table v-loading="pd.loading" :data="pd.processList" ref="refTable"
                  @select="select1" @select-all="selectAll">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="工序名称" align="center" prop="name"/>
          <el-table-column label="工艺类别" align="center" prop="treeValue">
            <template #default="scope">
              <dictTag :options="wms_process_tree" :value="scope.row.treeValue"/>
            </template>
          </el-table-column>
          <el-table-column label="工艺要求" align="center" prop="attention"/>
          <el-table-column label="是否起" align="center" prop="status" :formatter="formatStatus"/>
          <el-table-column label="创建部门" align="center" prop="createDept"/>
          <el-table-column label="备注" align="center" prop="remark"/>
          <el-table-column label="创建者" align="center" prop="createBy"/>
          <el-table-column label="创建时间" align="center" prop="createTime"/>
          <el-table-column label="更新者" align="center" prop="updateBy"/>
          <el-table-column label="更新时间" align="center" prop="updateTime"/>
        </el-table>
      </el-row>
      <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSelected">确 定</el-button>
          <el-button @click="pd.open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="lineSearch" lang="ts">
import {listProcess, ProcessVO} from "@/views/wms/process/api";

const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const { wms_process_tree } = toRefs<any>(proxy?.useDict('wms_process_tree' ));

const queryFormRef = ref<ElFormInstance>();
const props = defineProps({
  //组件类别。1是输入搜索框，2是按钮搜索框，3link文本样式弹出框，默认是1样式。
  searchType: {type: Number, default: 1},
  // 对话框标题
  title: {type: String, default: "工序查询"},
  // 是否单选
  isSingle: {type: Boolean, default: true},
  // 有监控，只要变化，控件自主修改
  dataText: {type: String, default: 'null'},
  // 数据过滤参数
  params: {type: Object, default: null}
});

const pd = reactive({
  processList: <ProcessVO[]>([]),
  reParams: <ProcessVO[]>([]),
  dataText: 'null',
  loading: false,
  open: false,
  total: 0,
  dialog: <DialogOption>{
    visible: false,
    title: ''
  },
  queryParams: <ProcessVO>{
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    treeId: undefined,
  }
})

function formatStatus(row: ProcessVO) {
  return row.status === '1' ? '是' : '否';
}

function select1(selection: ProcessVO[], row: ProcessVO[]) {
  //是否为单选模式
  if (props.isSingle) {
    //清空选项
    (proxy?.$refs["refTable"] as any).clearSelection();
  }
  //将这行选中
  (proxy?.$refs["refTable"] as any).toggleRowSelection(row, true);
}

function selectAll(selection: ProcessVO[]) {
  //是否为单选模式
  if (props.isSingle) {
    //清空选项
    (proxy?.$refs["refTable"] as any).clearSelection();
  }
}

/** 查询测试单列表 */
async function getList() {
  pd.loading = true;
  const res = await listProcess(pd.queryParams);
  pd.processList = res.rows;
  pd.total = res.total;
  pd.loading = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  (proxy?.$refs['queryFormRef'] as any).resetFields();
  getList();
}

defineExpose({
  initSearch,
});

const emit = defineEmits(["ok"]);

//数据变化页面需要变化,默认null
watch(() => props.dataText, val => {
  if (props.searchType === 2 || props.searchType === 4) {
    return;
  }
  if (props.dataText == undefined || props.dataText == '') {
    pd.dataText = 'null';
  } else {
    pd.dataText = props.dataText;
  }
}, {deep: true, immediate: true});

// 初始化1样式的展示
function initSearch() {
  pd.queryParams = {...pd.queryParams, ...props.params};
  handleQuery();
  pd.open = true;
}

/** 按钮选择操作 */
function handleSelected() {
    pd.reParams = (proxy?.$refs["refTable"] as any).getSelectionRows();
    if (pd.reParams.length === 0) {
      proxy?.$modal.msgError("请选择数据，至少一行!!");
      return;
    }
    pd.open = false;
    emit("ok", pd.reParams);
}

</script>
